package org.kfive.nova.thread;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 16-5-31
 * Time: 下午10:40
 * To change this template use File | Settings | File Templates.
 */
public class CountDownLatchExample {

    private CountDownLatch startGate = new CountDownLatch(1);

    private CountDownLatch endGate = new CountDownLatch(10);

    public void run(){
        for (int i=0;i<10;i++){
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    //To change body of implemented methods use File | Settings | File Templates.
                    try {
                        System.out.println(Thread.currentThread().getName()+"startGate.await");
                        startGate.await();
                        System.out.println(Thread.currentThread().getName()+"sleep 10000");
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName()+"startGate.complete");
                        endGate.countDown();
                    } catch (InterruptedException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            });
            t.start();
        }
        try {
        Thread.sleep(10000);
        System.out.println(Thread.currentThread().getName()+"startGate.countDown");
        startGate.countDown();

            System.out.println(Thread.currentThread().getName()+"endGate.await");
            endGate.await();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public static void main(String[] args) {
        CountDownLatchExample example = new CountDownLatchExample();
        example.run();
    }
}
